package com.ruoyi.web.controller.mes;


import java.util.List;
import java.util.Map;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.kt.mes.domain.ProcessRouteItem;
import com.kt.mes.service.IProcessRouteItemService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 工艺路线详情Controller
 *
 * @author lzc
 * @date 2021-01-18
 */
@RestController
@RequestMapping("/mes/pro_rot_item")
public class ProcessRouteItemController extends BaseController
{
    @Autowired
    private IProcessRouteItemService processRouteItemService;

    /**
     * 查询工艺路线详情列表
     */
    @PreAuthorize("@ss.hasPermi('mes:pro_rot_item:list')")
    @GetMapping("/list")
    public TableDataInfo list(ProcessRouteItem processRouteItem)
    {
        startPage();
        List<ProcessRouteItem> list = processRouteItemService.selectProcessRouteItemList(processRouteItem);
        return getDataTable(list);
    }

    /**
     * 导出工艺路线详情列表
     */
    @PreAuthorize("@ss.hasPermi('mes:pro_rot_item:export')")
    @Log(title = "工艺路线详情", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(ProcessRouteItem processRouteItem)
    {
        List<ProcessRouteItem> list = processRouteItemService.selectProcessRouteItemList(processRouteItem);
        ExcelUtil<ProcessRouteItem> util = new ExcelUtil<ProcessRouteItem>(ProcessRouteItem.class);
        return util.exportExcel(list, "pro_rot_item");
    }

    /**
     * 获取工艺路线详情详细信息
     */
    @PreAuthorize("@ss.hasPermi('mes:pro_rot_item:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(processRouteItemService.selectProcessRouteItemById(id));
    }

    /**
     * 新增工艺路线详情
     */
    @PreAuthorize("@ss.hasPermi('mes:pro_rot_item:add')")
    @Log(title = "工艺路线详情", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ProcessRouteItem processRouteItem)
    {

        ProcessRouteItem query=new ProcessRouteItem();
        query.setPrId(processRouteItem.getPrId());

       List<ProcessRouteItem>  list=processRouteItemService.selectProcessRouteItemList(query);
       if(list.isEmpty()==true){
           processRouteItem.setPriNum(1);
       }else{
           processRouteItem.setPriNum(list.size()+1);
       }

        return toAjax(processRouteItemService.insertProcessRouteItem(processRouteItem));
    }

    /**
     * 修改工艺路线详情
     */
    @PreAuthorize("@ss.hasPermi('mes:pro_rot_item:edit')")
    @Log(title = "工艺路线详情", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ProcessRouteItem processRouteItem)
    {
        System.out.println(processRouteItem);
        return toAjax(processRouteItemService.updateProcessRouteItem(processRouteItem));
    }

    /**
     * 删除工艺路线详情
     */
    @PreAuthorize("@ss.hasPermi('mes:pro_rot_item:remove')")
    @Log(title = "工艺路线详情", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(processRouteItemService.deleteProcessRouteItemByIds(ids));
    }


    //工艺项目树形结构
    @GetMapping("/treeselect")
    public AjaxResult treeselect(String prId)
    {

        return AjaxResult.success(processRouteItemService.buildProcessItemTreeSelect(prId));
    }

    @PreAuthorize("@ss.hasPermi('mes:pro_rot_item:edit')")
    @PutMapping("/update_order")
    public AjaxResult updateOrder(@RequestBody List<Map<String,Integer>> list)
    {


        list.forEach((v)->{
            System.out.println(v);
           ProcessRouteItem processRouteItem=new ProcessRouteItem();
           processRouteItem.setId(Long.valueOf(v.get("id")));
           processRouteItem.setPriNum(v.get("priNum"));
           processRouteItemService.updateProcessRouteItem(processRouteItem);

       });
        return  AjaxResult.success();
    }
    //添加工序以及参数




}
